using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace TankWar{ public partial class FormMain : Form { #region 私有字段 //游戏状态 private GameState _gameState = GameState.Close; //我方坦克 private Tank _myTank = new Tank(Side.Me); //敌方坦克集合 private List<Tank> _listEnemyTank = new List<Tank>(); //子弹的集合 private List<Bullet> _listBullet = new List<Bullet>(); #endregion //导入动态链接库 [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] public static extern short GetAsyncKeyState(int keyCode); public FormMain() { InitializeComponent(); } //开始游戏 private void BeginToolStripMenuItem_Click(object sender, EventArgs e) { _gameState = GameState.Open; //启用timer timer1.Enabled = true; timer2.Enabled = true; timer3.Enabled = true; timer4.Enabled = true; } //结束游戏 private void EndToolStripMenuItem_Click(object sender, EventArgs e) { _gameState = GameState.Close; //禁用timer timer1.Enabled = false; timer2.Enabled = false; } private void pictureBox1_Paint(object sender, PaintEventArgs e) { //绘制我方 _myTank.DrawMe(e.Graphics); //绘制敌方 for (int i = 0; i <= _listEnemyTank.Count - 1; i ) _listEnemyTank[i].DrawMe(e.Graphics); foreach (Bullet myBullet in _listBullet) { myBullet.DrawMe(e.Graphics); } } private void FormMain_KeyDown(object sender, KeyEventArgs e) { //如果开始游戏 if (_gameState == GameState.Open) { //开火 if (e.KeyCode == Keys.Space) { //我方坦克发射 Bullet myBullet = _myTank.Fire(); _listBullet.Add(myBullet); } //强制刷新 pictureBox1.Invalidate(); } } private void timer1_Tick(object sender, EventArgs e) { //如果开始游戏 if (_gameState == GameState.Open) { //先产生一辆敌方坦克 Tank enemyTank = new Tank(Side.Enemy); //把他加入列表 _listEnemyTank.Add(enemyTank); //强制刷新 pictureBox1.Invalidate(); } } private void timer2_Tick(object sender, EventArgs e) { //如果开始游戏 if (_gameState == GameState.Open) { //定义一个随机类 Random myRand = new Random(DateTime.Now.Second); //随机控制每一辆的运动方向 for (int i = 0; i <= _listEnemyTank.Count - 1; i ) { //产生一个随机数 int direction = myRand.Next(1, 50); //让坦克移动一个步长 if (direction >= 5 && direction <= 10) { if (_listEnemyTank[i]._Position.X >= _myTank._Position.X) _listEnemyTank[i].Move(Direction.Left); else _listEnemyTank[i].Move(Direction.Right); } else if (direction >= 11 && direction <= 15) { if (_listEnemyTank[i]._Position.Y >= _myTank._Position.Y) _listEnemyTank[i].Move(Direction.Up); else _listEnemyTank[i].Move(Direction.Down); } else if (direction >= 16 && direction <= 49) { _listEnemyTank[i].Move(_listEnemyTank[i]._Direction); } else { _listEnemyTank[i]._Direction = (Direction)direction; _listEnemyTank[i].Move(_listEnemyTank[i]._Direction); } } foreach (Bullet myBullet in _listBullet) { myBullet.Move(); } //强制刷新 pictureBox1.Invalidate(); } } private void timer3_Tick(object sender, EventArgs e) { //如果游戏开始了 if (_gameState == GameState.Open) { //定义一个随机类 Random myRand = new Random(DateTime.Now.Second); //让子弹飞 foreach (Tank enemyTank in _listEnemyTank) { //产生一个随机数 int fireFlag = myRand.Next(1, 10); //判断是否发射 if (fireFlag <= 6) { Bullet enemyBullet = enemyTank.Fire(); _listBullet.Add(enemyBullet); } } } } private void timer4_Tick(object sender, EventArgs e) { if (_gameState == GameState.Open) { bool keyDown = (((ushort)GetAsyncKeyState((int)Keys.Down)) & 0xffff) != 0; if (keyDown == true) _myTank.Move(Direction.Down); bool keyUp = (((ushort)GetAsyncKeyState((int)Keys.Up)) & 0xffff) != 0; if (keyUp == true) _myTank.Move(Direction.Up); bool keyLeft = (((ushort)GetAsyncKeyState((int)Keys.Left)) & 0xffff) != 0; if (keyLeft == true) _myTank.Move(Direction.Left); bool keyRight = (((ushort)GetAsyncKeyState((int)Keys.Right)) & 0xffff) != 0; if (keyRight == true) _myTank.Move(Direction.Right); } //强制刷新 pictureBox1.Invalidate(); } }}
下载C# 坦克大战 小游戏源码用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论